package Service;

import java.util.ArrayList;
import java.util.Iterator;

import javax.portlet.ActionRequest;

import models.Flag;
import models.Group;
import models.taggedParams;
import models.untaggedParams;

import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartRequest;

public class CMDHelper {
	
	private int numoffile =0;
	private ArrayList<String> filenames = new ArrayList<String>();
	
	public String buildCMD(GetServerInfo sinfo,ActionRequest request){
		
		boolean is_basic = sinfo.isBasic();
		String cmd="";
		if(is_basic == false){
			String fCmd ="";
			String tCmd = "";
			String uCmd = "";
			
			ArrayList<Group> gps = new ArrayList<Group>();
			gps = sinfo.getGroup_set();
			for(int i =0; i<gps.size();i++){
				fCmd += buildFlagCMD(sinfo, gps.get(i),request);
				tCmd += buildTaggedCMD(sinfo, gps.get(i),request);
				uCmd += buildUntaggedCMD(sinfo, gps.get(i),request);
			}
			cmd = fCmd+tCmd+uCmd;
		}
		else{
			cmd += request.getParameter("command");
			cmd = appendFName(cmd,request);
		}
		return cmd;
	}

	public String buildFlagCMD(GetServerInfo sinfo, Group gp,ActionRequest request){
		String cmd = "";
		ArrayList<Flag> flag_set = new ArrayList<Flag> ();
		flag_set = gp.getFlag_set();
		
		//loop through flag_set to check if that flag are checked. add to cmd if yes
		for(int i = 0; i< flag_set.size();i++){
				String flagid = flag_set.get(i).getId();
			    String flagtag = request.getParameter(flagid);
			    if (flagtag != null){
			    	
			    	//if gp is exclusive then check if it's the selected
			    	if(gp.isExclusive() == true){
			    		try{
				    		if(request.getParameter(gp.getName()).equals(flagid)){
				    			cmd += " "+flagtag;
				    		}
			    		}catch(NullPointerException e){
							//print log user didn't select			    			
			    		}
			    	}
			    	//else just add it into the command
			    	else{
			    		cmd += " "+flagtag;			    		
			    	}
			    }
		}
		
		return cmd;
	}
	
	public String buildTaggedCMD(GetServerInfo sinfo, Group gp,ActionRequest request){
		
		String cmd ="";
		ArrayList<taggedParams> tagged_set = new ArrayList<taggedParams>();
		String separator = sinfo.getSeparator();//get serparator for tag
		tagged_set = gp.getTagged_set();
		
		//loop through tagged set, add to cmd if selected.
		for(int i =0; i< tagged_set.size(); i++){
			
				String tag = tagged_set.get(i).getTag();
				String ptype = tagged_set.get(i).getPtype();
				String iotype = tagged_set.get(i).getIotype();
				String id = tagged_set.get(i).getId();
				
				//if it's the input is file and the iotype is input, then it's uplaoding file
				if(ptype.equals("FILE") && iotype.equals("INPUT")){
					String value = getFName(request,tag,gp);
					if(value != null && !value.equals("") ){
						
				    	//if gp is exclusive then check if it's the selected
						if(gp.isExclusive() == true){
							try{
								if(request.getParameter(gp.getName()).equals(id)){
									cmd += " " + tag + separator+value;	
								}
							}catch(NullPointerException e){
								//print log user didn't select
							}
						}
						else{
							cmd += " " + tag + separator+value;	
						}
					}
					else{
						//System.err.println("unfind file for: "+tag );
					}
				}
				else{
					String value = request.getParameter(tag);
					if(value != null && !value.equals("")){
				    	//if gp is exclusive then check if it's the selected
						if(gp.isExclusive() == true){
							try{
								if(request.getParameter(gp.getName()).equals(id)){
									cmd += " " + tag + separator+value;								
								}
							}catch(NullPointerException e){
								//print log user didn't select
							}
						}
						else{
							cmd += " " + tag + separator+value;
						}					
					}
				}
		}
		
		return cmd;
	}
	
	public String buildUntaggedCMD(GetServerInfo sinfo, Group gp,ActionRequest request){
		
		String cmd="";
		ArrayList<untaggedParams> untagged_set = new ArrayList<untaggedParams>();
		untagged_set = gp.getUntagged_set();
		
		//loop through untagged set, add to cmd cmd if selected
		for(int i =0; i< untagged_set.size(); i++){
			
			String ptype = untagged_set.get(i).getPtype();
			String iotype = untagged_set.get(i).getIotype();
			String id = untagged_set.get(i).getId();
			
			if(ptype.equals("FILE")&&iotype.equals("INPUT")){
				String value = getFName(request,id,gp);
				if(value != null && !value.equals("")){
			    	//if gp is exclusive then check if it's the selected
					if(gp.isExclusive() == true){
						try{
							if(request.getParameter(gp.getName()).equals(id)){
								cmd += " " +value;								
							}
						}catch(NullPointerException e){
							//ToDo: print log 
						}
						
					}
					else{
						cmd += " " +value;							
					}
				}
				else{
					//System.err.println("unfind file for: "+ id);
				}
			}
			else{
				String value = request.getParameter(untagged_set.get(i).getId());
				if(value != null && !value.equals("")){
			    	//if gp is exclusive then check if it's the selected
					if(gp.isExclusive() == true){
						try{
							if(request.getParameter(gp.getName()).equals(id)){
								cmd += " " +value;								
							}
						}catch(NullPointerException e){
							//ToDo: print log .. maybe user didn't select a value
						}
					}
					else{
						cmd += " " +value;							
					}				}
			}
		}
		return cmd;
	}
	
	public String getBasicFName(ActionRequest request, String name){
		String filename ="";
		MultipartRequest multiRequest = (MultipartRequest)request;
		MultipartFile file = multiRequest.getFile("uploadfile");
		filename = file.getOriginalFilename();
		numoffile++;
	    return filename;
	}
	
	private String getFName(ActionRequest request, String name,Group gp){
			String filename ="";
		   MultipartRequest multiRequest = (MultipartRequest)request;
	        Iterator it = multiRequest.getFileNames();
	        while(it.hasNext()){
	        	MultipartFile file = multiRequest.getFile((String)it.next());
	        	if( file.getName().equals(name) ){
	        		filename = file.getOriginalFilename();
	        		if(gp.isExclusive() ==true){
	        			if(request.getParameter(gp.getName()).equals(name)){
			        		filenames.add(file.getName());
			        		numoffile++;
	        			}
	        		}
	        		else{
		        		filenames.add(file.getName());
		        		numoffile++;
	        		}

	        	}
	        	
	        }
	        return filename;
	}
	
	private String appendFName(String cmd, ActionRequest request){
		MultipartRequest multiRequest = (MultipartRequest)request;
	    Iterator it = multiRequest.getFileNames();
        while(it.hasNext()){
        	MultipartFile file = multiRequest.getFile((String)it.next());
        	if(file.getOriginalFilename() != "" &&file.getOriginalFilename()!= null){
		    		cmd += " " + file.getOriginalFilename();
		    		filenames.add(file.getName());
		    		numoffile++;
        	}
        }
		return cmd;
	}

	public ArrayList<String> getFilenames() {
		return filenames;
	}

	public void setFilenames(ArrayList<String> filenames) {
		this.filenames = filenames;
	}

	public int getNumoffile() {
		return numoffile;
	}

	public void setNumoffile(int numoffile) {
		this.numoffile = numoffile;
	}
	
	
}
